#include "rsacrypt.h"

RsaCrypt::RsaCrypt()
{

}

QString RsaCrypt::rsa_pri_encrypt_base64(const QString &strClearData)
{
    //私钥  长度为512  （使用自己生成的公秘钥）
    char private_key[] = "-----BEGIN RSA PRIVATE KEY-----\n"\
                   "MIIBOwIBAAJBAKK+i+mOiflYG6HSPekRZwX5G2TUVTBnnUwYTb2CETS/F4GUKppa\n"\
                   "hWH3CakwgHRsHDt9+egfRJE1OIiNSFic+0kCAwEAAQJAHuEOxRMRpOkN3Sd6LcXq\n"\
                   "iVyZ0Qjk3Rlb5DtcTM/oGdy2xxcJ3OvgHjiYLhGcnqRKnGKrv7u5iP8NjAKuYKFp\n"\
                   "fQIhANKTIYl/KIVu1davQNSkNXzxlOuiiet5TI7Dj2qpfgXPAiEAxdoCNFM30zuP\n"\
                   "lCIwLUJuomdtKPxIophMVaEANHO7S2cCIQDBILYykBBPCM9Q/h08qXpxJHSMBN/v\n"\
                   "uqlvioafseHmWwIhAMIIW8EVG9p8vwJ3xOeeDwIWjGd1g4Zx9erESn8mQLhhAiA+\n"\
                   "7oLiB9b1ZPBYME3wT9qwkiPGAX0Fx0B9jYUXTOoN0Q==\n"\
                   "-----END RSA PRIVATE KEY-----";

       //将字符串键加载到bio对象
       BIO* pKeyBio = BIO_new_mem_buf(private_key, strlen(private_key));
       if (pKeyBio == NULL){
           return "";
       }
       RSA* pRsa = RSA_new();
       pRsa = PEM_read_bio_RSAPrivateKey(pKeyBio, &pRsa, NULL, NULL);
       if ( pRsa == NULL ){
            BIO_free_all(pKeyBio);
            return "";
       }
       int nLen = RSA_size(pRsa);
       char* pEncryptBuf = new char[nLen];
       memset(pEncryptBuf, 0, nLen);
       QByteArray clearDataArry = strClearData.toUtf8();
       int nClearDataLen = clearDataArry.length();
       uchar* pClearData = (uchar*)clearDataArry.data();
       int nSize = RSA_private_encrypt(nClearDataLen,
                                       pClearData,
                                       (uchar*)pEncryptBuf,
                                       pRsa,
                                       RSA_PKCS1_PADDING);

       QString strEncryptData = "";
       if ( nSize >= 0 ){
            QByteArray arry(pEncryptBuf, nSize);
            strEncryptData = arry.toBase64();
       }
       // 释放内存
       delete[] pEncryptBuf;
       BIO_free_all(pKeyBio);
       RSA_free(pRsa);
       return strEncryptData;

}

QString RsaCrypt::rsa_pub_decrypt_base64(const QString &strDecryptData)
{
    //公钥解密
        char public_key[] = "-----BEGIN PUBLIC KEY-----\n"\
                "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKK+i+mOiflYG6HSPekRZwX5G2TUVTBn\n"\
                "nUwYTb2CETS/F4GUKppahWH3CakwgHRsHDt9+egfRJE1OIiNSFic+0kCAwEAAQ==\n"\
                "-----END PUBLIC KEY-----";

        //将字符串键加载到bio对象
        BIO* pKeyBio = BIO_new_mem_buf(public_key, strlen(public_key));
        if (pKeyBio == NULL){
            return "";
        }

        RSA* pRsa = RSA_new();

        pRsa = PEM_read_bio_RSA_PUBKEY(pKeyBio, &pRsa, NULL, NULL);


        if ( pRsa == NULL ){
            BIO_free_all(pKeyBio);
            return "";
        }
        int nLen = RSA_size(pRsa);
        char* pClearBuf = new char[nLen];
        memset(pClearBuf, 0, nLen);
        //解密
        QByteArray decryptDataArry = strDecryptData.toUtf8();
        decryptDataArry = QByteArray::fromBase64(decryptDataArry);
        int nDecryptDataLen = decryptDataArry.length();
        uchar* pDecryptData = (uchar*)decryptDataArry.data();
        int nSize = RSA_public_decrypt(nDecryptDataLen,
                                       pDecryptData,
                                       (uchar*)pClearBuf,
                                       pRsa,
                                       RSA_PKCS1_PADDING);
        QString strClearData = "";
        if ( nSize >= 0 ){
            strClearData = QByteArray(pClearBuf, nSize);
        }

        // 释放内存
        delete[] pClearBuf;
        BIO_free_all(pKeyBio);
        RSA_free(pRsa);
        return strClearData;
}
